package com.java.proxy.cglib;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * @author liyanhao
 * @Date 2023-02-25 13:51
 */
public class Tank4 {

    public void move() {
        System.out.println("Tank moving clacla...");
    }

    public static void main(String[] args) {
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(Tank4.class);
        enhancer.setCallback(new TimeMethodInterceptor());
        Tank4 o = (Tank4)enhancer.create();
        o.move();
    }
}

class TimeMethodInterceptor implements MethodInterceptor {
    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        System.out.println("生成的类名:" + o.getClass().getName());
        System.out.println("生成的类的父类:" + o.getClass().getSuperclass().getName());
        System.out.println("方法执行前,被代理类的方法" + method.getName());
        Object result = null;
        result = methodProxy.invokeSuper(o, objects);
        System.out.println("方法执行后，被代理的方法:" + method.getName());
        return result;
    }
}
